package com.yanqu.road.server.agent.inter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eAgentErrorCode;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.RechargeModule;
import com.yanqu.road.server.gameplayer.module.player.SecretVolumeModule;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.logic.recharge.FundRecharge;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.SortedMap;
import java.util.TreeMap;

public class AgentBuy {
    public static String ThirdPartyCharge(JSONObject param){
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        SortedMap<String,String> paramMap = new TreeMap<>();
        long userId = param.getLong("userId");
        int productId = param.getInteger("productId");
        String sign = param.getString("sign");
        paramMap.put("userId",String.valueOf(userId));
        paramMap.put("productId",String.valueOf(productId));
        if(SignHelper.checkSign(paramMap, AppKeyMgr.getAndroidAppSecret(), sign)){
            GamePlayer gamePlayer = GamePlayerMgr.getPlayer(userId);
            if(gamePlayer != null){
                int res = gamePlayer.getModule(RechargeModule.class).backendRechargeMall(productId);
                if(res != 0){
                    AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.GetDataError.getValue());
                }else{
                    AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
                }
            }else{
                AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.PlayerOffline.getValue());
            }
        }else{
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.ArgumentError.getValue());
        }
        return ret.toString();
    }

    //后台购买财神密卷
    public static String SecretVolumeRecharge(JSONObject param){
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        String[] playerIds = param.getString("userId").split(",");
        boolean allSend = true;
        for (String userId : playerIds){
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(Long.valueOf(userId));
            if(player != null){
                SecretVolumeModule secretVolumeModule = player.getModule(SecretVolumeModule.class);
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(GameConfig.SECRET_VOLUME_MALL_ID);
                secretVolumeModule.buySecretVolume();
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.GM, eLogMoneyType.GmSend);
            }else{
                allSend = false;
            }
        }
        if(allSend){
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        }else {
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.PlayerOffline.getValue());
        }
        return ret.toString();
    }

    //后台购买门客基金
    public static String PatronsFundRecharge(JSONObject param){
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        String[] playerIds = param.getString("userId").split(",");
        boolean allSend = true;
        for (String userId : playerIds){
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(Long.valueOf(userId));
            if(player != null){
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(GameConfig.ACTIVITY_PATRONS_FUND);
                player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.PatronsFund, 1);
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.GM, eLogMoneyType.GmSend);
                //门客基金世界展示
                WorldMessageMgr.worldNotify(player, eMessageType.BuyPatronsFund.getValue());
            }else{
                allSend = false;
            }
        }
        if(allSend){
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        }else {
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.PlayerOffline.getValue());
        }
        return ret.toString();
    }

    //后台购买赚速基金
    public static String EarnSpeedFundRecharge(JSONObject param){
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        String[] playerIds = param.getString("userId").split(",");
        boolean allSend = true;
        for (String userId : playerIds){
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(Long.valueOf(userId));
            if(player != null){
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(GameConfig.ACTIVITY_EARNMONEY_FUND);
                player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.EarnSpeedFund, 1);
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.GM, eLogMoneyType.GmSend);
                FundRecharge.notifyAndLogEarnSpeedFund(player);
            }else{
                allSend = false;
            }
        }
        if(allSend){
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        }else {
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.PlayerOffline.getValue());
        }
        return ret.toString();
    }

    //后台购买精进赚钱基金
    public static String EarnSpeedFundPlusRecharge(JSONObject param) {
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        String[] playerIds = param.getString("userId").split(";");
        boolean allSend = true;
        for (String userId : playerIds) {
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(Long.valueOf(userId));
            if (player != null) {
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(GameConfig.ACTIVITY_EARNMONEY_PLUS_FUND);
                player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.EarnSpeedPlusFound, 1);
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.GM, eLogMoneyType.GmSend);
                FundRecharge.notifyAndLogEarnSpeedPlusFund(player);
            } else {
                allSend = false;
            }
        }
        if (allSend) {
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        } else {
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.PlayerOffline.getValue());
        }
        return ret.toString();
    }
}
